package com.iccaplayer;

import com.example.iccaplayer.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewParent;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class InsInfoActivity extends Activity {
	private static final String TAG = "InsInfoActivity";
	
	Intent mainIntent;
	Bundle extras;	
	
	int insPos;
	int drumKeys[] = new int[5];
	String insName, insType, insProg;
	
	// Primary Layout
	TextView insNameEdit;
	Spinner insTypeSpin;
	ArrayAdapter<CharSequence> typeArray;
	LinearLayout displayTypeLayout;
	Button okBtn, cancelBtn;
	
	// Instruments Type Layout
	LinearLayout classLayout, progLayout;
	TextView classText, progText;
	Spinner classSpin, progSpin;	
	ArrayAdapter<CharSequence> classArray, allArray, pianoArray, 
	chromaticArray, organArray, guitarArray, bassArray, stringsArray, ensembleArray,
	brassArray, reedArray, pipeArray, leadArray, padArray, sEffectsArray, ethnicArray,
	percussiveArray, effectArray;	
	
	// Drum Kits Type Layout
	LinearLayout drumLayout[] = new LinearLayout[5];
	TextView drumText[] = new TextView[5];
	Spinner drumSpin[] = new Spinner[5];
	ArrayAdapter<CharSequence> drumArray;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ins_info);	
		
		mainIntent = getIntent();
		extras = mainIntent.getExtras();		
		
		if(extras != null){
			insPos = extras.getInt("InsPos");
			insType = extras.getString("Type");
			insProg = extras.getString("Prog");
			if(insType.equalsIgnoreCase("Drum Kits")){
				drumKeys = extras.getIntArray("Keys");
			}
			insName = extras.getString("Name");
		}		
		
		createPrimaryLayout();		
		createInsTypeLayout();
		createDrumTypeLayout();
	}
	
	private void createPrimaryLayout(){
		insNameEdit = (EditText)findViewById(R.id.InsNameEditText);
		insNameEdit.setText(insName);
		
		insTypeSpin = (Spinner)findViewById(R.id.InsTypeSpin);
		typeArray = ArrayAdapter.createFromResource(this, R.array.TypeArray, android.R.layout.simple_spinner_item);
		typeArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		insTypeSpin.setAdapter(typeArray);
		if(insType.equalsIgnoreCase("Instruments")){
			insTypeSpin.setSelection(0);
		}
		else if(insType.equalsIgnoreCase("Drum Kits")){
			insTypeSpin.setSelection(1);
		}
		insTypeSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {		
				insType = insTypeSpin.getItemAtPosition(position) + "";
				switch (position) {
				case 0:
					addInsTypeLayout();
					break;
					
				case 1:
					insProg = insType;
					addDrumTypeLayout();
					break;

				default:
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {	
			}
		});		
		
		displayTypeLayout = (LinearLayout)findViewById(R.id.DisplayTypeLayout);
		
		// Close Information of Instrument Activity	
		okBtn = (Button)findViewById(R.id.InfoOkBtn);
		okBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mainIntent.putExtra("InsIndex", insPos);
				mainIntent.putExtra("InsName", insNameEdit.getText() + "");
				mainIntent.putExtra("InsType", insType);
				mainIntent.putExtra("InsProg", insProg);
				if(insType.equalsIgnoreCase("Drum Kits")){
					mainIntent.putExtra("DrumKeys", drumKeys);
				}
				setResult(RESULT_OK, mainIntent);
				finish();
			}
		});		
		
		// Close Information of Instrument Activity
		cancelBtn = (Button)findViewById(R.id.InfoCancelBtn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});		
	}
	
	private void createInsTypeLayout(){
		// Classification
		classLayout = new LinearLayout(this);
		
		classText = new TextView(this);
		classText.setText("Classification");
		
		classSpin = new Spinner(this);
		classArray = ArrayAdapter.createFromResource(this, R.array.SpecificationArray, android.R.layout.simple_spinner_item);
		classArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		classSpin.setAdapter(classArray);

		// Program of Instruments
		progLayout = new LinearLayout(this);
		
		progText = new TextView(this);
		progText.setText("Instruments: ");
		
		progSpin = new Spinner(this);
		allArray = ArrayAdapter.createFromResource(this, R.array.AllArray, android.R.layout.simple_spinner_item);
		pianoArray = ArrayAdapter.createFromResource(this, R.array.PianoArray, android.R.layout.simple_spinner_item);
		chromaticArray = ArrayAdapter.createFromResource(this, R.array.ChromaticArray, android.R.layout.simple_spinner_item);
		organArray = ArrayAdapter.createFromResource(this, R.array.OrganArray, android.R.layout.simple_spinner_item);
		guitarArray = ArrayAdapter.createFromResource(this, R.array.GuitarArray, android.R.layout.simple_spinner_item);
		bassArray = ArrayAdapter.createFromResource(this, R.array.BassArray, android.R.layout.simple_spinner_item);
		stringsArray = ArrayAdapter.createFromResource(this, R.array.StringsArray, android.R.layout.simple_spinner_item);
		ensembleArray = ArrayAdapter.createFromResource(this, R.array.EnsembleArray, android.R.layout.simple_spinner_item);
		brassArray = ArrayAdapter.createFromResource(this, R.array.BrassArray, android.R.layout.simple_spinner_item);
		reedArray = ArrayAdapter.createFromResource(this, R.array.ReedArray, android.R.layout.simple_spinner_item);
		pipeArray = ArrayAdapter.createFromResource(this, R.array.PipeArray, android.R.layout.simple_spinner_item);
		leadArray = ArrayAdapter.createFromResource(this, R.array.LeadArray, android.R.layout.simple_spinner_item);
		padArray = ArrayAdapter.createFromResource(this, R.array.PadArray, android.R.layout.simple_spinner_item);
		sEffectsArray = ArrayAdapter.createFromResource(this, R.array.SEffectsArray, android.R.layout.simple_spinner_item);
		ethnicArray = ArrayAdapter.createFromResource(this, R.array.EthnicArray, android.R.layout.simple_spinner_item);
		percussiveArray = ArrayAdapter.createFromResource(this, R.array.PercussiveArray, android.R.layout.simple_spinner_item);
		effectArray = ArrayAdapter.createFromResource(this, R.array.EffectArray, android.R.layout.simple_spinner_item);
	
		classSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				switch(position){
				case 0:
					progSpin.setAdapter(allArray);
					break;
				case 1:
					progSpin.setAdapter(pianoArray);
					break;
				case 2:
					progSpin.setAdapter(chromaticArray);
					break;
				case 3:
					progSpin.setAdapter(organArray);
					break;
				case 4:
					progSpin.setAdapter(guitarArray);
					break;
				case 5:
					progSpin.setAdapter(bassArray);
					break;
				case 6:
					progSpin.setAdapter(stringsArray);
					break;
				case 7:
					progSpin.setAdapter(ensembleArray);
					break;
				case 8:
					progSpin.setAdapter(brassArray);
					break;
				case 9:
					progSpin.setAdapter(reedArray);
					break;
				case 10:
					progSpin.setAdapter(pipeArray);
					break;
				case 11:
					progSpin.setAdapter(leadArray);
					break;
				case 12:
					progSpin.setAdapter(padArray);
					break;
				case 13:
					progSpin.setAdapter(sEffectsArray);
					break;
				case 14:
					progSpin.setAdapter(ethnicArray);
					break;
				case 15:
					progSpin.setAdapter(percussiveArray);
					break;
				case 16:
					progSpin.setAdapter(effectArray);
					break;	
				}
			}						

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {				
			}
		});
		
		progSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {	
				insProg = progSpin.getItemAtPosition(position) + "";
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {	
			}
		});
		
		classLayout.addView(classText);
		classLayout.addView(classSpin);
		progLayout.addView(progText);
		progLayout.addView(progSpin);		
	}
	
	private void addInsTypeLayout(){
		displayTypeLayout.removeAllViews();
		
		displayTypeLayout.addView(classLayout);
		displayTypeLayout.addView(progLayout);		
	}
	
	private void createDrumTypeLayout(){	
		drumArray = ArrayAdapter.createFromResource(this, R.array.DrumArray, android.R.layout.simple_spinner_item);
		drumArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		for(int pos=0; pos<5; pos++){
			drumLayout[pos] = new LinearLayout(this);
			
			drumText[pos] = new TextView(this);
			drumText[pos].setText((pos+1)+" : ");
			
			drumSpin[pos] = new Spinner(this);
			drumSpin[pos].setId(pos);
			Log.d(TAG, "spinId: " + drumSpin[pos].getId());
			drumSpin[pos].setAdapter(drumArray);
			drumSpin[pos].setSelection(drumKeys[pos] - 35);
			drumSpin[pos].setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View spin,
						int position, long arg3) {		
					int index;
					index = ((ViewGroup)spin.getParent()).getId();
					Log.d(TAG, "index: " + index);
					drumKeys[index] = position + 35;
					Log.d(TAG, "index: " + index + " key: " + drumKeys[index]);
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});
			
			drumLayout[pos].addView(drumText[pos]);
			drumLayout[pos].addView(drumSpin[pos]);
		}
	}
	
	private void addDrumTypeLayout(){
		displayTypeLayout.removeAllViews();
		
		for(int pos=0; pos<5; pos++){
			displayTypeLayout.addView(drumLayout[pos]);
		}
	}
}
